﻿function doLogin() {

	$.ajax({
		async : true,
		url : LOGIN_INTERFECE_URL + "?action=" + LOGIN_ACTION_NAME,
		data : {
			ywsloginname : objTxtUsername.attr('value'),
			password : objTxtPassword.attr('value')
		},
		type : 'POST',
		dataType : 'json',
		timeout : 20000,
		error : function(xmlhttprequest, errordata) {
			var errindex = 0;
			if(errordata == 'timeout')
				errindex = 99;
			
			login_callback({clientcode: errindex});
		},
		success : function(jsondata) {
			login_callback(jsondata);
		}
	});
//	if (objTxtUsername.attr("value") == "aaa") {
//	var a = {myinfo:{myheart:'aaatest',myid:33},groups:"aaa",clientcode:1};
//	login_callback(a);
//	return;
//}
}

function doSendChat(targetuserid, chatcontent){
	$.ajax({
		async : true,
		url : LOGIN_INTERFECE_URL + "?action=" + CHAT_ACTION_NAME,
		data : {
			targetuserid : targetuserid,
			chatcontent : chatcontent
		},
		type : 'POST'
	});
}

function doInterface(){
	$.ajax({
		async : true,
		url : INTERFACE_INTERFECE_URL,
		type : 'POST',		//用POST，防止缓存问题
		timeout : 3600000,	//一小时
		error : function(xmlhttprequest, errordata) {
			doInterface();
		},
		success : function(jsondata) {
			actionInterfaceResponse(jsondata);
			doInterface();
		}
	});
}

//管理Interface响应数据
function actionInterfaceResponse(jsondata){
	
	$(jsondata.messages).each(function(i, v){

		var evalStr = "do_" + v.messageType + "(v);";
		eval(evalStr);

	});

}

//处理收到的聊天信息
function do_chatcontent(value){
	var senderid = value.senderID;
	var it = getUserFromArray(senderid);

//	var chatWindow = document.getElementById("chatWindow_" + senderid);
//	if(chatWindow == null)
	showChatWindow(senderid);


	var chatContentItsNickname = document.createElement("div");
	$(chatContentItsNickname).addClass("chatContentItsNickname");
	var s = it.memo;
	if(s == "")
		s = it.nickname;
	$(chatContentItsNickname).html(s + " " + value.sendTime);
	var chatContentItsContente = document.createElement("div");
	$(chatContentItsContente).addClass("chatContent");
	$(chatContentItsContente).html(value.messageContent);
	
	var con = $("#chatContentShow_" + senderid);
	$(chatContentItsNickname).appendTo(con);
	$(chatContentItsContente).appendTo(con);
	
	con.animate({scrollTop: con[0].scrollHeight});


}

// 创建聊天窗体
function showChatWindow(userid) {

	var temp = document.getElementById("chatWindow_" + userid);

	// != null 有两种情况
	// 显示出来
	// 只在statebar显示
	if (temp != null) {
		if ($(temp).css("display") == "none")
			$("#statebar_" + userid).css("display", "none");
		$(temp).css("display", "block");
		$(temp).css("z-index", ++SYSTEM_Z_INDEX);

		return;
	}


	var currentUser = getUserFromArray(userid);

	var cw = document.createElement("div");
	$(cw).attr("id", "chatWindow_" + userid);
	$(cw).addClass("chatWindow");

	var wt = document.createElement("div");
	$(wt).attr("id", "windowTitle_" + userid);
	$(wt).addClass("windowTitle");

	var iface = document.createElement("img");
	$(iface).attr("src", "/image/userfaces/" + userid + ".jpg");
	$(iface).attr("title", (currentUser.isOnline ? "在线":"离线") + "，点击查看资料");
	$(iface).addClass("chatFormImage");

	$(wt).append($(iface));

	var nn = document.createElement("div");
	$(nn).addClass("chatformnickname");

	if(currentUser.memo == ""){
		$(nn).html(currentUser.nickname);
	}else{
		$(nn).html(currentUser.memo + "(" + currentUser.nickname + ")");
	}

	var sm = document.createElement("span");
	$(sm).attr("id", "tomin_" + userid);
	$(sm).attr("title", "最小化");
	$(sm).addClass("minbox");
	

	var sc = document.createElement("span");
	$(sc).attr("id", "close_" + userid);
	$(sc).attr("title", "关闭");
	$(sc).addClass("closebox");
//	$(nn).append($(sc));
//	$(nn).append($(sm));

	
	
	$(wt).append($(sc));
	$(wt).append($(sm));
	$(wt).append($(nn));

	var hea = document.createElement("div");
	$(hea).html(currentUser.heart);
	$(hea).addClass("chatformheart");

	$(wt).append($(hea));
	
	$(cw).append($(wt)); // WindowTitle完成

	var cs = document.createElement("div");
	$(cs).attr("id", "chatContentShow_" + userid);
	//$(cs).html("<span style='color:#0000FF;font-size:12px;'>螳臂挡车的歹徒 2010-11-18 15:44:55</span><br /><span style='color:#FF0000;font-size:22px;padding-left:25px;padding-bottom:20px;'>哈哈哈啊哈</span>");
	$(cs).addClass("chatContentShow");


	$(cw).append($(cs)); // ChatContentShow完成

	var fc = document.createElement("div");
	$(fc).addClass("fontControl");


	var c = document.createElement("span");
	$(c).addClass("changeColor");
	$(fc).append($(c));

	var w = document.createElement("span");
	$(w).addClass("changeWeight");
	$(fc).append($(w));

	var f = document.createElement("span");
	$(f).addClass("selectFace");
	$(fc).append($(f));

	$(cw).append($(fc)); // FontControl完成

	var cc = document.createElement("div");
	$(cc).attr("id", "enterChatContent_" + userid);
	$(cc).addClass("enterChatContent");
	// cc.contenteditable = "true";
	// cc.setAttribute("contenteditable", "true");
	$(cw).append($(cc)); // EnterChatContent完成

	var isend = document.createElement("img");

	$(isend).attr("id", "chatFormButton_" + userid);
	$(isend).attr("src", "/image/send_normal.png");
	$(isend).addClass("chatFormButton");
	$(cw).append($(isend)); // SendButton完成

	
	var iclear = document.createElement("img");
	$(iclear).attr("id", "chatFormClearButton_" + userid);
	$(iclear).attr("src", "/image/clearscreen_normal.png");
	$(iclear).addClass("chatFormClearButton");
	$(cw).append($(iclear)); // SendButton完成

	//document.body.appendChild(cw);
	//$("body").append($(cw));
	$(cw).appendTo(objBody);

	var newO = $("#chatWindow_" + userid);
	newO.easydrag();
	newO.setHandler("windowTitle_" + userid);
	
	$("#enterChatContent_" + userid).attr("contentEditable", "true");
	
	newO.css("z-index", ++SYSTEM_Z_INDEX);
	
}

function toMinPersonWindow(){
	var o = document.getElementById("stateImgPersonSetWindow");
	if( o != null){
		closePersonWindow();
		$(o).css("display", "inline");
		return;
	}
	
	
	
	var i = document.createElement("img");
	$(i).attr("id", "stateImgPersonSetWindow");
	//i.id = tmpID;
	$(i).addClass("statebarImage");
	//i.className = "statebarImage";
	$(i).attr("src", "/image/userfaces/" + MYINFO.myid + ".jpg");
	//i.src = "/image/userfaces/" + public_getIdFromString(windowid) + ".jpg";
	$(i).attr("title", "个人设置");
	$(i).appendTo(objStatebar);
	
	$("#personSetWindow").css("display", "none");
}
function closePersonWindow(){
	$("#personSetWindow").css("display", "none");
}
function toMin(windowid) {
	
	closeChatWIndow(windowid);
	var tmpID = "statebar_" + public_getIdFromString(windowid);

	if (document.getElementById(tmpID) != null) {
		$("#" + tmpID).css("display", "inline");
		return;
	}

	var i = document.createElement("img");
	$(i).attr("id", tmpID);
	//i.id = tmpID;
	$(i).addClass("statebarImage");
	//i.className = "statebarImage";
	$(i).attr("src", "/image/userfaces/" + public_getIdFromString(windowid) + ".jpg");
	//i.src = "/image/userfaces/" + public_getIdFromString(windowid) + ".jpg";
	var cu = getUserFromArray(windowid);
	var m = cu.memo;
	if(m == "")
		m = cu.nickname;
	$(i).attr("title", "与 " + m + " 聊天中...");
	$(i).appendTo(objStatebar);
	//objStatebar.append($(i));
	//document.getElementById("statebar").appendChild(i);

}

function closeChatWIndow(windowid) {
	$("#chatWindow_" + windowid).css("display", "none");
}



function flashStateBarImage(id) {
	var o = $("#statebar_" + id);
	setTimeout();
}

//创建个人设置窗体
function showSetPersonWindow(){
	var o = document.getElementById("personSetWindow");
	if( o != null){
		if($(o).css("display") == "none")	//在状态栏
			$("#stateImgPersonSetWindow").css("display", "none");
		$(o).css("display", "block");
		$(o).css("z-index", ++SYSTEM_Z_INDEX);

		return;
	}
	
	var personSetWindow = document.createElement("div");
	$(personSetWindow).attr("id", "personSetWindow");
	$(personSetWindow).addClass("personSetWindow");
	
	
	var personWindowTitle = document.createElement("div");
	$(personWindowTitle).attr("id", "personWindowTitle");
	$(personWindowTitle).addClass("personWindowTitle");

	var sc = document.createElement("span");
	$(sc).attr("id", "personWindowClose");
	$(sc).attr("title", "关闭");
	$(sc).addClass("closebox");
	$(sc).appendTo($(personWindowTitle));
	
	var sm = document.createElement("span");
	$(sm).attr("id", "personWindowMin");
	$(sm).attr("title", "最小化");
	$(sm).addClass("minbox");
	$(sm).appendTo($(personWindowTitle));
	
	var imgPersonWindow = document.createElement("img");
	$(imgPersonWindow).attr("src", "/image/userfaces/" + MYINFO.myid + ".jpg");
	$(imgPersonWindow).addClass("serPersonFormFace");
	
	var spanTitle = document.createElement("div");
	$(spanTitle).addClass("personWindowTitleSpan");
	$(spanTitle).html("个人设置");
	
	$(imgPersonWindow).appendTo($(personWindowTitle));
	$(spanTitle).appendTo($(personWindowTitle));

	
	$(personWindowTitle).appendTo($(personSetWindow));
	
	var personWindowTabsBar =  document.createElement("div");
	
	var btnMyPersonInfo = document.createElement("button");
	$(btnMyPersonInfo).attr("id", "btnMyPersonInfo");
	$(btnMyPersonInfo).html("我的资料");
	$(btnMyPersonInfo).addClass("tabsBarButton tabsBarButtonactive");

	$(btnMyPersonInfo).appendTo($(personWindowTabsBar));
	
	var btnFriendManage = document.createElement("button");
	$(btnFriendManage).attr("id", "btnFriendManage");
	$(btnFriendManage).html("好友管理");
	$(btnFriendManage).addClass("tabsBarButton");
	$(btnFriendManage).appendTo($(personWindowTabsBar));
	
	var btnSystemSet = document.createElement("button");
	$(btnSystemSet).attr("id", "btnSystemSet");
	$(btnSystemSet).html("系统设置");
	$(btnSystemSet).addClass("tabsBarButton");
	$(btnSystemSet).appendTo($(personWindowTabsBar));
	
	$(personWindowTabsBar).addClass("personWindowTabsBar");
	$(personWindowTabsBar).appendTo($(personSetWindow));
	
	var divMyPersonInfo = document.createElement("div");
	$(divMyPersonInfo).attr("id", "divMyPersonInfo");
	$(divMyPersonInfo).html("content我的资料");
	$(divMyPersonInfo).addClass("tabsContent");
	$(divMyPersonInfo).css("display", "block");
	$(divMyPersonInfo).appendTo($(personSetWindow));
	
	var divFriendManage = document.createElement("div");
	$(divFriendManage).attr("id", "divFriendManage");
	$(divFriendManage).html("content好友管理");
	$(divFriendManage).addClass("tabsContent");
	$(divFriendManage).appendTo($(personSetWindow));
	
	var divSystemSet = document.createElement("div");
	$(divSystemSet).attr("id", "divSystemSet");
	$(divSystemSet).html("content系统设置");
	$(divSystemSet).addClass("tabsContent");
	$(divSystemSet).appendTo($(personSetWindow));
	
	$(personSetWindow).appendTo(objBody);

	
	//允许拖动
	$("#personSetWindow").easydrag();
	$("#personSetWindow").setHandler("personWindowTitle");

	$("#personSetWindow").css("z-index", ++SYSTEM_Z_INDEX);

}
